<?php


namespace app\controller\admin\statistics;


use app\BaseController;
use app\model\partner\EscortMenuReserveOrderModel;
use app\model\partner\UserPartnerModel;
use app\model\user\PartnerMoneyRecordModel;
use app\model\user\UserModel;
use app\service\user\PartnerMoneyRecordService;
use think\db\Query;

class IncomeController extends BaseController
{

    public function getList()
    {
        try {
            $where = [];
            $mobile = $this->request->param('mobile', '', 'trim');
            if ($mobile) {
                $where[] = ['mobile', 'like', "%{$mobile}%"];
            }
            $cityId = $this->request->param('city_id', '', 'trim');
            if ($cityId) {
                $where[] = ['city', '=', "{$cityId}"];
            }
            $id = $this->request->param('id', '', 'trim');
            if ($id) {
                $where[] = ['id', '=', "{$id}"];
            }
            $createTime = $this->request->param('create_time', '', 'trim');
            if ($createTime) {
                $startTime = strtotime($createTime[0]);
                $endTime = strtotime($createTime[1]) + 86400;
            } else {
                $startTime = 0;
                $endTime = time();
            }

            $orderBy = $this->request->param('order_by', '', 'trim');
            $sort = $this->request->param('sort', '', 'trim');
            if (!$orderBy) {
                $moneyList = PartnerMoneyRecordModel::field('sum(amount) as total_amount,user_id')
                    ->whereIn('type', [1, 3, 4])
                    ->where('create_time', '>=', $startTime)
                    ->where('create_time', '<', $endTime)
                    ->group('user_id')
                    ->order('total_amount', 'desc')
                    ->select()
                    ->toArray();
                $idArr = array_column($moneyList, 'user_id');
                $otherIdArr = UserPartnerModel::whereNotIn('id', $idArr)->column('id');
                $idArr = array_merge($idArr, $otherIdArr);
                $order = implode(',', $idArr);
                $userMoneyList = [];
                foreach ($moneyList as $value) {
                    $userMoneyList[$value['user_id']] = $value['total_amount'];
                }
            } else {
                $order = [$orderBy => $sort];
            }

            $list = UserPartnerModel::where($where)
                ->field('id,user_type,mobile,username,ratio')
                ->when(!$orderBy, function (Query $query) use ($order) {
                    $query->orderRaw("field(id,$order)");
                }, function (Query $query) use ($order) {
                    $query->order($order);
                })
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                // 接单数
                $acceptOrderList = EscortMenuReserveOrderModel::where('coach_user_id', $v['id'])
                    ->whereIn('status', [3, 5, 7, 8])
                    ->where('accept_time', '>=', $startTime)
                    ->where('accept_time', '<=', $endTime)
                    ->select()
                    ->toArray();
                $list['data'][$k]['accept_order_num'] = count($acceptOrderList);
                // 总时长
                $list['data'][$k]['total_duration'] = array_sum(array_column($acceptOrderList, 'duration'));
                // 总收入
                $list['data'][$k]['income_amount'] = $userMoneyList[$v['id']] ?? 0;

            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}